import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter_taotaoshipin/app/app_controller.dart';
import 'package:flutter_taotaoshipin/http/request/user_cancel_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_drama_auth_list_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_drama_collect_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_drama_log_list_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_drama_record_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_login_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_logout_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_open_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_pay_log_list_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_phone_login_request.dart';
import 'package:flutter_taotaoshipin/http/request/user_reg_request.dart';
import 'package:flutter_taotaoshipin/model/base_entity.dart';
import 'package:flutter_taotaoshipin/model/phone_login_entity.dart';
import 'package:flutter_taotaoshipin/model/user_cancel_entity.dart';
import 'package:flutter_taotaoshipin/model/user_drama_auth_list_entity.dart';
import 'package:flutter_taotaoshipin/model/user_drama_collect_entity.dart';
import 'package:flutter_taotaoshipin/model/user_drama_log_list_entity.dart';
import 'package:flutter_taotaoshipin/model/user_drama_record_entity.dart';
import 'package:flutter_taotaoshipin/model/user_login_entity.dart';
import 'package:flutter_taotaoshipin/model/user_logout_entity.dart';
import 'package:flutter_taotaoshipin/model/user_open_entity.dart';
import 'package:flutter_taotaoshipin/model/user_pay_log_list_entity.dart';
import 'package:flutter_taotaoshipin/pages/login/local_user_info.dart';
import 'package:flutter_taotaoshipin/util/device_utils.dart';
import 'package:flutter_taotaoshipin/value/constant.dart';
import 'package:get/get.dart';
import 'package:hi_net/core/hi_error.dart';
import 'package:hi_net/hi_net.dart';

class UserDao {
  /// 初始化接口
  static Future<BaseEntity<UserOpenEntity>> open() async {
    var request = UserOpenRequest();
    request.add('from', Constant.from);
    request.add('udid', Device.udid());
    request.add('ua', Device.userAgent());
    request.add('model', Device.model());
    request.add('oaid', Device.oaid());
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserOpenEntity>.fromJson(result);
  }

  /// 用户登陆接口
  static Future<BaseEntity<UserLoginEntity>> login(
    String user,
    String password,
  ) async {
    AppController appController = Get.find();
    var request = UserLoginRequest();
    request.add('user', user);
    request.add('passwd', password);
    request.add('lver', appController.appProfile.ver!);
    request.add('from', Constant.from);
    request.add('udid', Device.udid());
    request.add('ua', Device.userAgent());
    request.add('model', Device.model());
    request.add('oaid', Device.oaid());
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserLoginEntity>.fromJson(result);
  }

  /// 用户注册接口
  static Future<BaseEntity<UserLoginEntity>> register(
    String user,
    String password,
    String rePassword,
    String mobile,
  ) async {
    AppController appController = Get.find();
    var request = UserRegRequest();
    request.add('user', user);
    request.add('passwd', password);
    request.add('re_passwd', rePassword);
    request.add('mobile', mobile);
    request.add('from', Constant.from);
    request.add('rver', appController.appProfile.ver!);
    request.add('udid', Device.udid());
    request.add('ua', Device.userAgent());
    request.add('model', Device.model());
    request.add('oaid', Device.oaid());
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserLoginEntity>.fromJson(result);
  }

  /// 获取用户的分集观看权限列表
  static Future<BaseEntity<List<UserDramaAuthListEntity>>> dramaAuthList(
      int id) async {
    var request = UserDramaAuthListRequest();
    request.add('id', id);
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<List<UserDramaAuthListEntity>>.fromJson(result);
  }

  /// 用户充值日志列表
  static Future<BaseEntity<List<UserPayLogListEntity>>> payLogList() async {
    var request = UserPayLogListRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<List<UserPayLogListEntity>>.fromJson(result);
  }

  /// 用户消费记录
  static Future<BaseEntity<List<UserDramaLogListEntity>>> dramaLogList() async {
    var request = UserDramaLogListRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<List<UserDramaLogListEntity>>.fromJson(result);
  }

  /// 用户看剧历史
  static Future<BaseEntity<List<UserDramaRecordEntity>>> dramaRecord() async {
    var request = UserDramaRecordRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<List<UserDramaRecordEntity>>.fromJson(result);
  }

  /// 用户收藏追剧
  static Future<BaseEntity<List<UserDramaCollectEntity>>> dramaCollect() async {
    var request = UserDramaCollectRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<List<UserDramaCollectEntity>>.fromJson(result);
  }

  /// 退出登录
  static Future<BaseEntity<UserLogoutEntity>> logout() async {
    var request = UserLogoutRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserLogoutEntity>.fromJson(result);
  }

  /// 注销账号
  static Future<BaseEntity<UserCancelEntity>> cancel() async {
    var request = UserCancelRequest();
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<UserCancelEntity>.fromJson(result);
  }

  /// 用户手机验证码登陆接口
  static Future<BaseEntity<PhoneLoginEntity>> phoneLogin({
    required String phone,
    required String code,
  }) async {
    AppController appController = Get.find();
    var request = UserPhoneLoginRequest();
    request.add('phone', phone);
    request.add('code', code);
    request.add('lver', appController.appProfile.ver!);
    request.add('from', Constant.from);
    request.add('udid', Device.udid());
    request.add('ua', Device.userAgent());
    request.add('model', Device.model());
    request.add('oaid', Device.oaid());
    var result = await HiNet.getInstance().fire(request);
    return BaseEntity<PhoneLoginEntity>.fromJson(result);
  }

  /// 刷新登录信息
  static Future<void> refreshLogin() async {
    print('刷新登录');
    AppController appController = Get.find();
    var user = appController.localUserInfo.user;
    var pwd = appController.localUserInfo.password;
    if (user != null && pwd != null) {
      try {
        var result = await UserDao.login(user, pwd);
        if (result.code == 0 && result.data != null) {
          // 请求结果成功，数据不为空
          if (result.data!.id != null && result.data!.sessionId != null) {
            // 保存本地
            var userInfo = LocalUserInfo2(
              user: user,
              password: pwd,
              mId: result.data!.id.toString(),
              sessionId: result.data!.sessionId,
            );
            var appController = Get.find<AppController>();
            appController.updateLocaleUserinfo(userInfo);

            // 更新归因
            var chnId = result.data?.dye?.chnId;
            if (chnId != null && chnId != 0) {
              appController.appProfile.chnId = chnId;
            }
            var ver = result.data?.dye?.ver;
            if (ver != null && ver != '') {
              appController.appProfile.ver = ver;
            }
            var agentName = result.data?.dye?.agentName;
            if (agentName != null && agentName != '') {
              appController.appProfile.agentName = agentName;
            }
            appController.updateAppProfile(appController.appProfile);
            return;
          } else {
            LogUtil.d('刷新登录：数据不完整');
          }
        } else {
          LogUtil.d(result.message);
        }
      } on HiNetError catch (e) {
        LogUtil.d(e.message);
      } catch (e) {
        LogUtil.d(e.toString());
      }
    }
  }
}
